package com.spzx.user.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.api.domain.UserBrowseHistory;
import com.spzx.user.service.IUserBrowseHistoryService;

/**
 * 用户浏览记录Service业务层处理
 *
 * @author atguigu
 * @date 2024-11-01
 */
@Service
public class UserBrowseHistoryServiceImpl extends ServiceImpl<UserBrowseHistoryMapper, UserBrowseHistory> implements IUserBrowseHistoryService
{
    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;

    /**
     * 查询用户浏览记录列表
     *
     * @param userBrowseHistory 用户浏览记录
     * @return 用户浏览记录
     */
    @Override
    public List<UserBrowseHistory> selectUserBrowseHistoryList(UserBrowseHistory userBrowseHistory)
    {
        return userBrowseHistoryMapper.selectUserBrowseHistoryList(userBrowseHistory);
    }

    @Override
    public void saveUserBrowseHistory(UserBrowseHistory userBrowseHistory) {
        //根据用户id和skuId查询数据库中是否已经有该记录
        UserBrowseHistory userBrowseHistoryDataBase = userBrowseHistoryMapper.selectOne(new LambdaQueryWrapper<UserBrowseHistory>().eq(UserBrowseHistory::getUserId, userBrowseHistory.getUserId()).eq(UserBrowseHistory::getSkuId, userBrowseHistory.getSkuId()));
        //非空校验
        if (userBrowseHistoryDataBase!=null){
            //原来已经浏览器过该商品
            UserBrowseHistory userBrowseHistory1 = new UserBrowseHistory();
            //设置主键
            userBrowseHistory1.setId(userBrowseHistoryDataBase.getId());
            //设置更新时间
            userBrowseHistory1.setUpdateTime(new Date());
            //调用根据id更新的方法
            baseMapper.updateById(userBrowseHistory1);
        }else {
            //直接添加
            baseMapper.insert(userBrowseHistory);
        }



    }

}
